const std = @import("std");
const MyError = error{ GenericError, InvalidArgsError, DBError, APIError };
const LikeAnObject = struct {
    x: f64,
    y: f64,
    value: i32 = 12,
    fn print(self: *LikeAnObject) void {
        std.debug.print("value: {}\n", .{self.value});
    }
};
const GameType = enum { GAME, DLC, PACK };
const y = -49;
fn bar() i32 {
    return y;
}
fn foo(x: i32) void {
    var a: i64 = bar();
    std.debug.print(" x: {}, a: {}\n", .{ x, a });
    _ = bar();
}
pub fn main() void {
    var x: i32 = 45;
    foo(x);
    var obj = LikeAnObject{ .y = 1.0, .x = 2.0, .value = 47 };
    obj.print();
    // struct
    std.debug.print("obj: {}\n", .{obj});
    // enum
    std.debug.print("Game: {}, DLC?: {}, PACK: {}\n", .{ GameType.GAME, GameType.DLC, @enumToInt(GameType.PACK) });
    // arr
    var array = [_]u32{ 47, 48, 49 };
    var slice: []u32 = array[0..3];
    std.debug.print("array: {any}, 0: {}, slice: {any}\n", .{ array, array[0], slice });

    // string utf-8
    const string = "hello 世界";
    const world = "world";
    var sslice: []const u8 = string[0..5];
    std.debug.print("string {s}\n", .{string});
    std.debug.print("length {}\n", .{string.len});
    std.debug.print("null {}\n", .{world[5]});
    std.debug.print("slice: {s}\n", .{sslice});
    std.debug.print("huh?: {s}\n", .{string[0..9]});

    // for
    for (array) |value, index| {
        std.debug.print("array: {}:{}\n", .{ index, value });
    }

    // error
    wrap_maybe_error(0);
    wrap_maybe_error(100);
    // _ = maybe_error(0) catch |err| {
    //     std.debug.print("error: {}\n", .{err});
    // };
}
// error
fn wrap_maybe_error(v: i32) void {
    if (maybe_error(v)) |value| {
        std.debug.print("wrap maybe error value: {}\n", .{value});
    } else |err| {
        std.debug.print("wrap error: {}\n", .{err});
    }
}
// error
fn maybe_error(v: i32) !i32 {
    if (v == 0) return MyError.InvalidArgsError;
    return v;
}
